using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CreateSkillWindow : MonoBehaviour {
	
	public UIPopupList keyStat;
	public UICheckbox ArmorCheckPenalty;
	public UICheckbox TrainedOnly;
	public UILabel Name;
	public UILabel Description;
	
	public GameObject OKButton;
	public GameObject CancelButton;
	
	public GameObject target;
	public string functionName;
	
	string origName;
	Skill s;
	
	bool loaded = false;

	// Use this for initialization
	void Start () {
		if(!loaded)
		{
			UIButtonMessage message = OKButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "OKClicked";
			
			message = CancelButton.AddComponent<UIButtonMessage>();
			message.target = gameObject;
			message.functionName = "CancelClicked";
		}
	}
	
	public void LoadSkillDisplay(string skill, bool noEdit = false)
	{
		s = SkillManager.Load(skill);
		
		Debug.Log(skill);
		
		keyStat.selection = s.stat;
		ArmorCheckPenalty.isChecked = s.armorCheckPenalty;
		TrainedOnly.isChecked = s.trainedOnly;
		Description.text = s.description;
		Name.text = s.name;
		origName = skill;
		
		loaded = true;
			
		if(noEdit)
		{
			NGUITools.AddWidgetCollider(gameObject);
			
			foreach(Collider l in gameObject.GetComponentsInChildren<Collider>())
			{
				l.enabled = false;
			}
			
			collider.enabled = true;
			
			CancelButton.SetActiveRecursively(false);
			
			UIButtonMessage message = OKButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "CancelClicked";
			message.collider.enabled = true;
		}
		else
		{
			foreach(UIInput l in gameObject.GetComponentsInChildren<UIInput>())
			{
				l.text = l.label.text;
				l.mDefaultText = "Skill Info";
			}
			
			UIButtonMessage message = OKButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "SaveChangesClicked";
			
			CancelButton.GetComponentInChildren<UILabel>().text = "Delete!";
			message = CancelButton.AddComponent<UIButtonMessage>();		
			message.target = gameObject;
			message.functionName = "DeleteClicked";
		}
	}
	
	void OKClicked()
	{
		Skill s = SkillManager.AddSkill(Name.text, Description.text, keyStat.selection, ArmorCheckPenalty.isChecked, TrainedOnly.isChecked);
		
		if(target != null)
			target.SendMessage(functionName, new KeyValuePair<string,string>(s.name, Description.text), SendMessageOptions.DontRequireReceiver);
	
		Destroy(gameObject);
	}
	
	void SaveChangesClicked()
	{
		s.name = Name.text;
		s.description = Description.text;
		s.stat = keyStat.selection;
		s.armorCheckPenalty = ArmorCheckPenalty.isChecked;
		s.trainedOnly = TrainedOnly.isChecked;
		
		if(SkillManager.SaveSkill(s, origName))
		{
			target.SendMessage("renamedObject", new KeyValuePair<string, string>(origName, s.name), SendMessageOptions.DontRequireReceiver);
			
			Destroy(gameObject);
		}
	}
	
	void CancelClicked()
	{
		Destroy(gameObject);
	}
	
	IEnumerator DeleteClicked()
	{
		List<bool> b = new List<bool>{false};
		
		yield return StartCoroutine(HelperFunctions.ConfirmationWindow("Are you sure you want to delete " + origName + "?", b));
		
		if(b[0])
		{
			SkillManager.DeleteSkill(origName);
			
			if(target != null)
				target.SendMessage(functionName);
		}
		
		Destroy(gameObject);
	}
}
